package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbToastUtil;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.util.YanZhengUtil;

/**
 * Created by Administrator on 2016/8/27/027.
 */
public class FindPassActivity  extends GBaseActivity {
    private LinearLayout ll1;
    private EditText editText_login_num;
    private ImageView imageViewIconError;
    private ImageView imageViewYanZhengMa;
    private EditText editText_login_password;
    private ImageView imageViewIcnoEye;
    private TextView textViewGetYanZhengMa;
    private LinearLayout ll2;
    private ImageView imageViewPass1;
    private EditText editTextPass1;
    private ImageView imageViewPass2;
    private EditText editTextPass2;
    private TextView textViewSumit;
    private CountDownTimer countDownTimer;
    private int step=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_findpass);
        findViewsById();
        initData();
    }

    private void findViewsById()
    {
        ll1 = (LinearLayout) findViewById(R.id.ll1);
        editText_login_num = (EditText) findViewById(R.id.editText_login_num);editText_login_num.setOnClickListener(this);
        imageViewIconError = (ImageView) findViewById(R.id.imageViewIconError);imageViewIconError.setOnClickListener(this);
        imageViewYanZhengMa = (ImageView) findViewById(R.id.imageViewYanZhengMa);
        editText_login_password = (EditText) findViewById(R.id.editText_login_password);editText_login_password.setOnClickListener(this);
        imageViewIcnoEye = (ImageView) findViewById(R.id.imageViewIcnoEye);imageViewIcnoEye.setOnClickListener(this);
        textViewGetYanZhengMa = (TextView) findViewById(R.id.textViewGetYanZhengMa);textViewGetYanZhengMa.setOnClickListener(this);
        ll2 = (LinearLayout) findViewById(R.id.ll2);
        imageViewPass1 = (ImageView) findViewById(R.id.imageViewPass1);
        editTextPass1 = (EditText) findViewById(R.id.editTextPass1);
        imageViewPass2 = (ImageView) findViewById(R.id.imageViewPass2);
        editTextPass2 = (EditText) findViewById(R.id.editTextPass2);
        textViewSumit = (TextView) findViewById(R.id.textViewSumit);textViewSumit.setOnClickListener(this);
    }

    private void initData()
    {
        setTopBarBackGround(getResources().getColor(R.color.white));
        setLeftImg(View.VISIBLE,R.drawable.icon_back_green);
        setTopTitle("找回密码");
        getTopTitle().setTextColor(getResources().getColor((R.color.color_4b4f52)));
        ll2.setVisibility(View.GONE);
        textViewSumit.setText("下一步");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewSumit:
                //1是下一步，2是提交修改密码
                if (step==1) {
                    if (YanZhengUtil.isMobileNO(editText_login_num.getText().toString()) && !TextUtils.isEmpty(editText_login_password.getText())) {
                       TaskCheckYanZhengMa(false,FindPassActivity.this,editText_login_num.getText().toString(),editText_login_password.getText().toString());
                    } else {
                        AbToastUtil.showToast(this, "格式错误，请重新输入");
                    }
                }else
                {
                    if (editTextPass1.getText().toString().equals(editTextPass1.getText().toString())) {
                        TaskUpdatePass(false,FindPassActivity.this,editText_login_num.getText().toString(),editTextPass1.getText().toString(),editText_login_password.getText().toString());
                    } else {
                        AbToastUtil.showToast(this, "2次密码输入不一致");
                    }
                }
                break;
            case R.id.imageViewIconError:
                editText_login_num.setText("");
                break;


            case R.id.textViewGetYanZhengMa:
                if (YanZhengUtil.isMobileNO(editText_login_num.getText().toString())) {
                   RegisterActivity. TaskGetYanZhengMa(false,FindPassActivity.this, editText_login_num.getText().toString(), "2");//1注册，2找回密码
                    countDownTimer = new CountDownTimer(60 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            textViewGetYanZhengMa.setText((int) (millisUntilFinished / 1000) + "秒后可重发");
                            textViewGetYanZhengMa.setAlpha(0.5f);
                            textViewGetYanZhengMa.setEnabled(false);

                        }

                        @Override
                        public void onFinish() {
                            textViewGetYanZhengMa.setText("获取验证码");
                            textViewGetYanZhengMa.setEnabled(true);
                            textViewGetYanZhengMa.setAlpha(1f);
                        }
                    }.start();
                }else
                {
                    ToastUtil.showToast("请正确输入手机号码");
                }
                break;
        }
    }

    public  void TaskCheckYanZhengMa(final boolean isloadmore,GBaseActivity activity ,String mobile,String yanZhengMa) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(activity, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_ChehckYanZheng, Constant.Action.Action_ChehckYanZheng, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_ChehckYanZheng,mobile, yanZhengMa), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        setTopTitle("重置密码");
                        ll1.setVisibility(View.GONE);
                        ll2.setVisibility(View.VISIBLE);
                        textViewSumit.setText("提交");
                        step=2;
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    public  void TaskUpdatePass(final boolean isloadmore, final GBaseActivity activity , String mobile, String pass, String yanZhengMa) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(activity, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_PassReset, Constant.Action.Action_PassReset, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_PassReset,mobile,pass, yanZhengMa), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                       ToastUtil.showToast("修改成功");
                        Intent intent=new Intent(activity,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (step==2)
        {

            setTopTitle("找回密码");
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.GONE);
            textViewSumit.setText("下一步");
        }
    }
}
